package nspel

import (
	"github.com/kataras/golog"
	"github.com/kataras/iris"
	rotateLogs "github.com/lestrrat/go-file-rotatelogs"
	"path"
	"runtime"
	"strconv"
	"time"
)

var logger *golog.Logger

func configLog (app *iris.Application) {
	logger = app.Logger()
	var data = struct {
		Debug		bool		`json:"debug"`
		LogFile	string	`json:"logFile"`
	}{}
	loadConfigJson("./config/log.json", &data)
	if !data.Debug {
		appLog(data.LogFile, app)
	}
}


func appLog (logFile string, app *iris.Application) {
	writer, err := rotateLogs.New(
		logFile + ".%Y%m%d%H%M",
		rotateLogs.WithLinkName(logFile),
		rotateLogs.WithMaxAge(7 * 24 * time.Hour),
		rotateLogs.WithRotationTime(24 * time.Hour))
	if err != nil {
		panic(err)
	}
	app.Logger().SetOutput(writer)
}

func Error (arg ...interface{}) {
	_, file, line, ok := runtime.Caller(1)
	if !ok {
		file = "???"
		line = 0
	}
	_, filename := path.Split(file)
	msg := "[" + filename + ":" + strconv.Itoa(line) + "] "
	logger.Error(msg, arg)
}
